import java.util.*;

public class War {
    public static void main(String[] args){
        War w = new War();
    }

    private List<Integer> a;
    private List<Integer> b;
    private boolean debug = false;

    public War()
    {
        //Set up structure
        a = new ArrayList<Integer>();
        b = new ArrayList<Integer>();

        //Deal cards
        int count = StdIn.readInt();
        for(int i = 0; i<count; i++){
            a.add(i,this.getValue(StdIn.readString()));
        }
        for(int i = 0; i<count; i++){
            b.add(i,this.getValue(StdIn.readString()));
        }
        
        //Play game
        int i = 0;
        if(debug) StdOut.println("Start piles:");
        if(debug) this.printPiles();
        if(debug) StdOut.println();
        while(!a.isEmpty() && !b.isEmpty() && i<100000){
            this.play();
            i++;
        }
        if(i<100000){
            if(b.isEmpty()){
                StdOut.println("A wins after "+i+" steps.");
            }else{
                StdOut.println("B wins after "+i+" steps.");
            }
        }else{
            StdOut.println("It's a tie after 100.000 steps");
        }
    }

    //Play the next cards from the top.
    private void play()
    {
        int val_a = a.get(0);
        int val_b = b.get(0);
        a.remove(0);
        b.remove(0);
        if(debug) StdOut.println("A played: "+val_a+", B played: "+val_b);
        if(val_a>val_b){
            a.add(val_a);
            a.add(val_b);
            if(debug) StdOut.println("A wins this one.");
        }else if(val_a<val_b){
            b.add(val_a);
            b.add(val_b);
            if(debug) StdOut.println("B wins this one.");
        }else{
            if(debug) StdOut.println("War!");
            int winner = 0;
            ArrayList<Integer> temp = new ArrayList<Integer>();
            temp.add(val_a);
            temp.add(val_b);
            int i = 1;
            while(winner==0 && !a.isEmpty() && !b.isEmpty()){
                if(i%2==0 && a.get(0)!=b.get(0)){
                    winner = a.get(0)>b.get(0) ? 1 : 2;
                }
                temp.add(a.get(0));
                a.remove(0);
                temp.add(b.get(0));
                b.remove(0);
                i++;
            }
            if(winner==0 && a.isEmpty() && b.isEmpty()){
                //Both players are out of cards and it's a tie. The game is finished.
                //This might be taken better care of, but input data seems kind of rare?
            }else if(winner==0){
                //One player ran out of cards. The game is finished.
            }else if(winner==1){
                for(int n : temp){
                    a.add(n);
                }
                if(debug) StdOut.println("A wins the war");
            }else if(winner==2){
                for(int n : temp){
                    b.add(n);
                }
                if(debug) StdOut.println("B wins the war");
            }
        }
        if(debug) StdOut.println("Piles:");
        if(debug) this.printPiles();
        if(debug) StdOut.println("##Round##");
    }

    //Import method; convert string to number..
    private int getValue(String in)
    {
        String val = in.substring(0,1);
        if(val.equals("A")){
            return 1;
        }else if(val.equals("T")){
            return 10;
        }else if(val.equals("J")){
            return 11;
        }else if(val.equals("Q")){
            return 12;
        }else if(val.equals("K")){
            return 13;
        }else{
            return Integer.parseInt(val);
        }
    }

    //Debug method; output the piles as they currently are..
    public void printPiles(){
        StdOut.println("Pile A has:");
        for(int i : this.a){
            StdOut.print(i+" ");
        }
        StdOut.println();
        StdOut.println("Pile B has:");
        for(int i : this.b){
            StdOut.print(i+" ");
        }
        StdOut.println();
    }
}
